package export.bullet
{
	import base.BaseBullet;
	import base.BaseObject;
	import com.dusk.util.McUtil;
	import flash.geom.Point;
	
	public class EnemyMoveBullet extends BaseBullet
	{
		
		public function EnemyMoveBullet(sourceName:String)
		{
			super(sourceName);
			isDestroyWhenLastFrame = false;
		}
		
		protected var funcWhenDistanceOver:Function;
		protected var moveTarget:BaseObject = null;
		protected var distanceLeft:Number = 1000;
		
		override public function render(ratio:Number = 1):void
		{
			if (moveTarget)
			{
				if (moveTarget.isDead() || moveTarget.isReadyToDestroy)
				{
					moveTarget = null;
				}
				else
				{
					var angle:Point = McUtil.getTwoObjectAngle(this, moveTarget);
					var sp:Number = getSpeedValue();
					velocity.x = angle.x * sp;
					velocity.y = angle.y * sp;
				}
			}
			super.render(ratio);
			distanceLeft -= getSpeedValue();
			if(distanceLeft <= 0)
			{
				if (funcWhenDistanceOver != null)
				{
					funcWhenDistanceOver(x, y);
				}
				destroy();
			}
		}
		
		public function setMoveTarget(target:BaseObject):void
		{
			moveTarget = target;
		}
		
		public function getMoveTarget():BaseObject
		{
			return moveTarget;
		}
		
		/**
		 * 设置主人时自动初始化位置
		 * @param o
		 */
		override public function setOwner(o:BaseObject):void
		{
			x = o.x;
			y = o.y;
			super.setOwner(o);
		}
		
		public function setPosition(posX:Number, posY:Number):void
		{
			x = posX;
			y = posY;
		}
		
		public function setDistance(dist:Number, callBack:Function = null):void
		{
			distanceLeft = dist;
			isDestroyWhenLastFrame = false;
			funcWhenDistanceOver = callBack;
		}
	}
}
